<?php

class Admin_CommentController extends JControllerAdmin
{
    /**
     * @return void
     */
    public function init()
    {
    }

    /**
     * @return void
     */
    public function indexAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $messages = $flashMessenger->getMessages();
        $this->view->messages = $messages;

        // BEGIN: Tạo đối tượng Paginator
        $dbSelect = JDbAdapter::factory()
            ->select()
            ->from('COMMENTS')
            ->where('Deleted = ?', false);

        // Tìm kiếm theo nội dung Comment (nếu có)
        $keyWords = $this->_request->getParam('keyWords');
        if (isset($keyWords)) {
            $dbSelect->where("Content LIKE '%$keyWords%'");
            $this->view->enteredKeyWords = $keyWords;
        }

        // Lọc các Comment đã hoặc chưa xử lý (nếu có)
        $confirmed = $this->_request->getParam('confirmed');
        if (isset($confirmed)) {
            $dbSelect->where('Confirmed = ?', (bool)$confirmed);
            $this->view->confirmed = (bool)$confirmed;
        }

        $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect($dbSelect)
        );
        // END: Tạo đối tượng Paginator

        // Get and set current page
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        $comments = $paginator->getItemsByPage($page);

        // Asign data for view
        $this->view->comments = $comments;
        $this->view->paginator = $paginator;
    }

    /**
     * @return void
     */
    public function addAction()
    {
    }

    /**
     * @return void
     */
    public function deleteAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $isPost = $this->_request->getParam('isPost');
        if (!isset($isPost)) {
            $flashMessenger->addMessage('Cannot Execute This URL Directly');
            $this->redirect('/admin/comment/index');
        }

        $commentIDs = $this->_request->getParam('commentIDs');
        if (!Application_Model_Comment::deleteComments($commentIDs)) {
            $flashMessenger->addMessage('Delete Fail');
        } else {
            $var = '';

            $length = count($commentIDs);
            for ($i = 0; $i < $length - 1; $i++) {
                $var .= $commentIDs[$i] . ', ';
            }
            $var .= $commentIDs[$length - 1];

            $flashMessenger->addMessage("Delete $var Successfully");
        }

        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    /**
     * @return void
     */
    public function updateAction()
    {
    }

    /**
     * @return void
     */
    public function confirmAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $isPost = $this->_request->getParam('isPost');
        if (!isset($isPost)) {
            $flashMessenger->addMessage('Cannot Execute This URL Directly');
            $this->redirect('/admin/comment/index');
        }

        $commentIDs = $this->_request->getParam('commentIDs');
        if (!Application_Model_Comment::confirmComments($commentIDs)) {
            $flashMessenger->addMessage('Confirm Fail');
        } else {
            $var = '';

            $length = count($commentIDs);
            for ($i = 0; $i < $length - 1; $i++) {
                $var .= $commentIDs[$i] . ', ';
            }
            $var .= $commentIDs[$length - 1];

            $flashMessenger->addMessage("Confirm $var Successfully");
        }

        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    /**
     * @return void
     */
    public function unconfirmAction()
    {
        /** @var $flashMessenger Zend_Controller_Action_Helper_FlashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $isPost = $this->_request->getParam('isPost');
        if (!isset($isPost)) {
            $flashMessenger->addMessage('Cannot Execute This URL Directly');
            $this->redirect('/admin/comment/index');
        }

        $commentID = $this->_request->getParam('commentID');
        if (!Application_Model_Comment::unconfirmComment($commentID)) {
            $flashMessenger->addMessage('Unconfirm Fail');
        } else {
            $flashMessenger->addMessage("Unconfirm $commentID Successfully");
        }

        $this->redirect($_SERVER['HTTP_REFERER']);
    }
}

